﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Utils
{
    public class Utils
    {
        static Dictionary<string, string> releaseGroups = new Dictionary<string, string>() { 
{"0TV","1"},
{"0×0008","1"},
{"1920","1"},
{"1KING","1"},
{"1REAL","1"},
{"1way","1"},
{"211","1"},
{"2HD","1"},
{"320","1"},
{"420RipZ","1"},
{"433","1"},
{"4CHOON","1"},
{"aAF","1"},
{"ACCiDENT","1"},
{"ACCLAiM","1"},
{"ADDiCTiON","1"},
{"ADHD","1"},
{"ADK","1"},
{"AEN","1"},
{"AEROHOLiCS","1"},
{"AFO","1"},
{"AG","1"},
{"AGaiN","1"},
{"ALKi","1"},
{"ALLiANCE","1"},
{"Allstars","1"},
{"AMIABLE","1"},
{"AMOK","1"},
{"AMSTEL","1"},
{"ANARCHY","1"},
{"ANiVCD","1"},
{"aPC","1"},
{"ARiGOLD","1"},
{"ARMC","1"},
{"ARROW","1"},
{"ARTHOUSE","1"},
{"AsiSter","1"},
{"ASS","1"},
{"ASSASS1NS","1"},
{"aTerFalleT","1"},
{"ATRium","1"},
{"ATS","1"},
{"AVCHD","1"},
{"AVENGED","1"},
{"AVS720","1"},
{"aWake","1"},
{"AzNiNVASiAN","1"},
{"B2R","1"},
{"BaCo","1"},
{"BAHAMUT","1"},
{"BAJSKORV","1"},
{"BaLD","1"},
{"Ballerina","1"},
{"BamHD","1"},
{"BAND1D0S","1"},
{"BaraTV","1"},
{"BAVARiA","1"},
{"BAWLS","1"},
{"BBDvDR","1"},
{"BBV","1"},
{"BCC","1"},
{"BDiSC","1"},
{"BDFLiX","1"},
{"BDP","1"},
{"BEAN","1"},
{"BERC","1"},
{"BeStDivX","1"},
{"BestHD","1"},
{"BETAMAX","1"},
{"BF","1"},
{"BFHMP3","1"},
{"BiA","1"},
{"BiEN","1"},
{"BiERBUiKEN","1"},
{"BiFOS","1"},
{"BiLDERBERG","1"},
{"BiQ","1"},
{"BLiZZARD","1"},
{"BLooDWeiSeR","1"},
{"BountyHunters","1"},
{"BOW","1"},
{"BPM","1"},
{"BRASTEMP","1"},
{"BREiN","1"},
{"BReWErS","1"},
{"BrG","1"},
{"BSiDE","1"},
{"BSP","1"},
{"BULLDOZER","1"},
{"BURGER","1"},
{"BWA","1"},
{"C4","1"},
{"CAMELOT","1"},
{"CAMERA","1"},
{"CarVeR","1"},
{"CBR","1"},
{"CCAT","1"},
{"CDA","1"},
{"Centropy","1"},
{"CFE","1"},
{"Chakra","1"},
{"CHaWiN","1"},
{"CHC","1"},
{"CHRONiC","1"},
{"CHRONO","1"},
{"CiELO","1"},
{"CiNEFiLE","1"},
{"Cinemaniacs","1"},
{"CiNEVCD","1"},
{"CiRCLE","1"},
{"CLASS","1"},
{"CLASSiC","1"},
{"CLEAR","1"},
{"CLONECD","1"},
{"CMC","1"},
{"CME","1"},
{"cNLDVDR","1"},
{"COALiTiON","1"},
{"COCAIN","1"},
{"COCMP3","1"},
{"CONDITION","1"},
{"C0NFUSED","1"},
{"CONFUSiON","1"},
{"CORE","1"},
{"COUNCiL","1"},
{"Counterfeit","1"},
{"CRD","1"},
{"CREMOSA","1"},
{"CRiMSON","1"},
{"CoWRY","1"},
{"CRBS","1"},
{"CTU","1"},
{"CYBERMEN","1"},
{"CzW","1"},
{"D2H","1"},
{"D3Si","1"},
{"DAGGER","1"},
{"DARKFORCE","1"},
{"DeBCz","1"},
{"DCP","1"},
{"DD","1"},
{"DDU","1"},
{"DDS","1"},
{"DELiGHT","1"},
{"DEViANCE","1"},
{"DFA","1"},
{"DGAS","1"},
{"DGN","1"},
{"DGX","1"},
{"dh","1"},
{"DHD","1"},
{"DiAMOND","1"},
{"DiCH","1"},
{"DIE","1"},
{"DiFFERENT","1"},
{"DIGERATI","1"},
{"DIMENSION","1"},
{"DiSSENT","1"},
{"DivXfacTory","1"},
{"DJ","1"},
{"dL","1"},
{"DMT","1"},
{"DNA","1"},
{"DnB","1"},
{"DOCUMENT","1"},
{"DoD","1"},
{"DOH","1"},
{"DOHTEM","1"},
{"DOMiNO","1"},
{"DoNE","1"},
{"DOORN","1"},
{"DoR2","1"},
{"DOT","1"},
{"dOve","1"},
{"DOWN","1"},
{"DPiMP","1"},
{"DRABBiTS","1"},
{"DREAMLiGHT","1"},
{"DRG","1"},
{"DROiDS","1"},
{"DTFS","1"},
{"DV8","1"},
{"DvF","1"},
{"DVT","1"},
{"DWM","1"},
{"DYNASTY","1"},
{"E7","1"},
{"ECHELON","1"},
{"ECHOS","1"},
{"EiTheLMP3","1"},
{"EGM","1"},
{"ELiA","1"},
{"EM","1"},
{"EMM","1"},
{"EMBRACE","1"},
{"EMERALD","1"},
{"EMF","1"},
{"EMXMP3","1"},
{"EnDoR","1"},
{"EOS","1"},
{"EPiCFAIL","1"},
{"EPiSODE","1"},
{"ESPiSE","1"},
{"eST","1"},
{"ETACH","1"},
{"ETHNiC","1"},
{"EXIST3NC3","1"},
{"EXViDiNT","1"},
{"F4CG","1"},
{"FA","1"},
{"FASDOX","1"},
{"FASiSO","1"},
{"FiCO","1"},
{"FiH","1"},
{"FiRM","1"},
{"FSiHD","1"},
{"FIXIT","1"},
{"FLAiR","1"},
{"FliK","1"},
{"FLS","1"},
{"FLT","1"},
{"FM","1"},
{"FMC","1"},
{"FmE","1"},
{"FMR","1"},
{"FNT","1"},
{"FoA","1"},
{"FOAD","1"},
{"Football","1"},
{"FoV","1"},
{"FQM","1"},
{"FQS","1"},
{"FRAGMENT","1"},
{"FTC","1"},
{"FTP","1"},
{"FUA","1"},
{"FUNCLUB2K9","1"},
{"FWYH","1"},
{"FZERO","1"},
{"GAMEOVER","1"},
{"GAYGAY","1"},
{"GDR","1"},
{"GENESIDE","1"},
{"GENiUS","1"},
{"GENUiNE","1"},
{"gF","1"},
{"GL","1"},
{"GLoBAL","1"},
{"GM4F","1"},
{"GNARLY","1"},
{"gnvr","1"},
{"GOOGLE","1"},
{"GORE","1"},
{"GOW","1"},
{"GRW","1"},
{"GT4","1"},
{"H2O","1"},
{"H3X","1"},
{"H5N1","1"},
{"HACO","1"},
{"HAGGiS","1"},
{"HAFVCD","1"},
{"HBD","1"},
{"hbZ","1"},
{"HDCP","1"},
{"HDi","1"},
{"HDP","1"},
{"HEH","1"},
{"HFT","1"},
{"HLS","1"},
{"hM","1"},
{"HML","1"},
{"Homely","1"},
{"HooKah","1"},
{"HOODLUM","1"},
{"HQEM","1"},
{"HSALiVE","1"},
{"hV","1"},
{"Hype","1"},
{"hXe","1"},
{"i8","1"},
{"iAK","1"},
{"iCON","1"},
{"iDC","1"},
{"iDT","1"},
{"iFN","1"},
{"iGNiTE","1"},
{"IGUANA","1"},
{"iHATE","1"},
{"iHF","1"},
{"iHQREV","1"},
{"iHT","1"},
{"iKA","1"},
{"iLG","1"},
{"iLS","1"},
{"iMBT","1"},
{"iMMERSiON","1"},
{"iMMORTALs","1"},
{"iMST","1"},
{"IMT","1"},
{"iNC","1"},
{"iNCiTE","1"},
{"iNCLUSION","1"},
{"iNFAMOUS","1"},
{"iNGOT","1"},
{"iNKiSO","1"},
{"iNTiMiD","1"},
{"InYa","1"},
{"iPC","1"},
{"iScen","1"},
{"iSG","1"},
{"iSO","1"},
{"ISOGER","1"},
{"iTALiVE","1"},
{"Jackal","1"},
{"JACKVID","1"},
{"JAH","1"},
{"Japhson","1"},
{"JFKXVID","1"},
{"JiM","1"},
{"JKR","1"},
{"JMT","1"},
{"JUPiLER","1"},
{"JUST","1"},
{"KALiSTO","1"},
{"KAMERA","1"},
{"KART3LDVD","1"},
{"KFC","1"},
{"KICKOFF","1"},
{"knk","1"},
{"KTMP3","1"},
{"KYR","1"},
{"KzT","1"},
{"LAJ","1"},
{"Larceny","1"},
{"LiNE","1"},
{"LionsDen","1"},
{"LiR","1"},
{"LMAO","1"},
{"LMG","1"},
{"LND","1"},
{"LoCAL","1"},
{"LoKI","1"},
{"LOL","1"},
{"LUSO","1"},
{"Lz0","1"},
{"LZY","1"},
{"M4L","1"},
{"McCain","1"},
{"MEDDY","1"},
{"MEDiAMANiACS","1"},
{"Metroid","1"},
{"MiNDTRiP","1"},
{"MiNiSTRY","1"},
{"MiNT","1"},
{"Misty","1"},
{"MiRAGETV","1"},
{"MiRROR","1"},
{"MK2","1"},
{"MoA","1"},
{"MOD","1"},
{"MoH","1"},
{"MOMENTUM","1"},
{"MOONCUBE","1"},
{"MORiCH","1"},
{"MOTION","1"},
{"MPX","1"},
{"MTC","1"},
{"MTD","1"},
{"MUSiQ","1"},
{"mV4U","1"},
{"MVA","1"},
{"MVM","1"},
{"MVN","1"},
{"MVS","1"},
{"MYCEL","1"},
{"MYTH","1"},
{"NBD","1"},
{"NeDiVx","1"},
{"NEPTUNE","1"},
{"NER","1"},
{"NeoX","1"},
{"NGR","1"},
{"NHH","1"},
{"NiSHiNO","1"},
{"NLSGDVDr","1"},
{"NODLABS","1"},
{"NOsegmenT","1"},
{"NOTV","1"},
{"NVS","1"},
{"NWO","1"},
{"OBC","1"},
{"OCTi","1"},
{"OE","1"},
{"OMA","1"},
{"OMERTA","1"},
{"OMiCRON","1"},
{"0MNi","1"},
{"0MNiDVD","1"},
{"ONe","1"},
{"OPiUM","1"},
{"OPTiC","1"},
{"ORC","1"},
{"ORGASM","1"},
{"ORENJi","1"},
{"ORGANiC","1"},
{"OSiTV","1"},
{"Ouzo","1"},
{"PALACE","1"},
{"PANTHEON","1"},
{"PARADOX","1"},
{"PaTHe","1"},
{"PBS","1"},
{"PC","1"},
{"PE","1"},
{"Pfooh","1"},
{"PiX","1"},
{"Pl","1"},
{"pLAN9","1"},
{"PmV","1"},
{"POKERUS","1"},
{"PORNOLATiON","1"},
{"POSTMORTEM","1"},
{"PoT","1"},
{"PoW","1"},
{"PROJECTX","1"},
{"PROPHET","1"},
{"PROMiSE","1"},
{"Pr0nStarS","1"},
{"PROTOCOL","1"},
{"PROViSiON","1"},
{"PROXY","1"},
{"PsyCZ","1"},
{"pSyPSP","1"},
{"PTC","1"},
{"PUKKA","1"},
{"PULSE","1"},
{"PusherCrew","1"},
{"PVR","1"},
{"PwA","1"},
{"pyt","1"},
{"QB","1"},
{"QMI","1"},
{"QQ","1"},
{"QTXMp3","1"},
{"QUASAR","1"},
{"R35","1"},
{"R3D","1"},
{"R3QU3ST","1"},
{"R6","1"},
{"RAC","1"},
{"RAGEMP3","1"},
{"RAiN","1"},
{"RAP","1"},
{"Razor1911/RazorDOX","1"},
{"RBS","1"},
{"REFiNED","1"},
{"REGEXP","1"},
{"REiGN","1"},
{"REiSERADiO","1"},
{"RELEASE","1"},
{"RELOADED","1"},
{"REMAX","1"},
{"Replica","1"},
{"RESiSTANCE","1"},
{"RESTORE","1"},
{"RETREAT","1"},
{"ReUnion","1"},
{"Rev0","1"},
{"rH","1"},
{"RIDDIM","1"},
{"RiFF","1"},
{"RiOT","1"},
{"Rise","1"},
{"RiTALiN","1"},
{"RiVER","1"},
{"RKcTV","1"},
{"RNS","1"},
{"RRR","1"},
{"RTA","1"},
{"RTB","1"},
{"RUBY","1"},
{"RUSH","1"},
{"RUSTLE","1"},
{"SAPHiRE","1"},
{"SAiNTS","1"},
{"SC","1"},
{"SCARED","1"},
{"SCREAM","1"},
{"SDR","1"},
{"SDS","1"},
{"SEPTiC","1"},
{"Sex4Ever","1"},
{"SexSh0p","1"},
{"SKA","1"},
{"SFM","1"},
{"SHELTER","1"},
{"SHOCK","1"},
{"SHooTERS","1"},
{"SiBERiA","1"},
{"SiMPLiFY","1"},
{"SiNNERS","1"},
{"SiQ","1"},
{"SiSO","1"},
{"SiTV","1"},
{"SKIDROW","1"},
{"SL","1"},
{"SMuT","1"},
{"SNOOK","1"},
{"SnS","1"},
{"SOF","1"},
{"SomeTV","1"},
{"SOULFUL","1"},
{"SPARE","1"},
{"SPAREL","1"},
{"SQ","1"},
{"SRG","1"},
{"SRP","1"},
{"SSF","1"},
{"SSR","1"},
{"STARCUBE","1"},
{"STELLA","1"},
{"STINKYCUBE","1"},
{"STRANGE","1"},
{"STRiKE","1"},
{"STRONG","1"},
{"StyleZz","1"},
{"SUBTiTLES","1"},
{"SUPERiER","1"},
{"SuperX360","1"},
{"SuPReME","1"},
{"SVD","1"},
{"SWAG","1"},
{"SWE6RUS","1"},
{"SYNDIKAT","1"},
{"SYS","1"},
{"T33V33","1"},
{"TBE","1"},
{"TBS","1"},
{"TCLUB","1"},
{"TCM","1"},
{"TDA","1"},
{"TDF","1"},
{"TDT","1"},
{"TDUJAM","1"},
{"TEKATE","1"},
{"TESORO","1"},
{"TE","1"},
{"TELEViSiON","1"},
{"TFiN","1"},
{"TGP","1"},
{"TheBatman","1"},
{"TheWretched","1"},
{"THG","1"},
{"TiiX","1"},
{"TiMELORDS","1"},
{"TiNYiSO","1"},
{"TKO","1"},
{"TNT","1"},
{"TOKUS","1"},
{"T0nK4","1"},
{"TPRoxxTheHouse","1"},
{"TRa","1"},
{"TRG","1"},
{"TRSI","1"},
{"TSP","1"},
{"TvD","1"},
{"TVFi","1"},
{"TVTUPA","1"},
{"TWiST","1"},
{"TwoPhat","1"},
{"UBE","1"},
{"UBiK","1"},
{"UBR","1"},
{"uC","1"},
{"uCF","1"},
{"UKHx","1"},
{"ULF","1"},
{"UME","1"},
{"UMF","1"},
{"Unique","1"},
{"Unleashed","1"},
{"UNSKiLLED","1"},
{"UNTOUCHED","1"},
{"UPE","1"},
{"USA","1"},
{"USF","1"},
{"USR","1"},
{"UTZ","1"},
{"VAG","1"},
{"VALiOMEDiA","1"},
{"VCDVaULT","1"},
{"Vcore","1"},
{"VENGEANCE","1"},
{"VERiTAS","1"},
{"VHPROD","1"},
{"VideoCD","1"},
{"ViRiLiTY","1"},
{"ViTE","1"},
{"ViTALiTY","1"},
{"VOA","1"},
{"VOiCE","1"},
{"VOLDieS","1"},
{"VoMiT","1"},
{"VORTEX","1"},
{"VST","1"},
{"W3D","1"},
{"W4F","1"},
{"WaLMaRT","1"},
{"WAGNER","1"},
{"WAR3X","1"},
{"WARG","1"},
{"WastedTime","1"},
{"WATERS","1"},
{"WAVE","1"},
{"wAx","1"},
{"WHOA","1"},
{"WhoKnow","1"},
{"WinBeta","1"},
{"WiS","1"},
{"WLM","1"},
{"WPi","1"},
{"WRD","1"},
{"WRE","1"},
{"WTW","1"},
{"WUS","1"},
{"x360inT","1"},
{"XanaX","1"},
{"XaRT","1"},
{"xDOMiNiONx","1"},
{"XiSO","1"},
{"XOR","1"},
{"XOXO","1"},
{"Xplode","1"},
{"XPRESS","1"},
{"XR5","1"},
{"xSCR","1"},
{"XSTREEM","1"},
{"XTC","1"},
{"xV","1"},
{"XXL","1"},
{"XXw","1"},
{"YCDV","1"},
{"YesTV","1"},
{"YPOGEiOS","1"},
{"YT","1"},
{"ZER0","1"},
{"ZRY","1"},
{"ZWTiSO","1"},
{"ZzZz","1"}       
        };

        public static bool AllWordsIncluded(string fullString, string stringToBeSplitted)
        {
            string[] words = stringToBeSplitted.Split(' ');
            foreach (string word in words)
            {
                if (word.ToLower().Equals("heb"))
                    continue;

                if (!fullString.ToLower().Contains(word.ToLower()))
                    return false;
            }

            return true;
        }

        public static string RemoveReleaseName(string searchString)
        {
            int pos = searchString.LastIndexOf(" ");
            if (pos == -1)
                return searchString;

            string lastWord = searchString.Substring(pos).Trim();
            if (releaseGroups.ContainsKey(lastWord))
                return searchString.Substring(0, pos);
            else
                return searchString;
        }

        public static string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

        public static int RunShell(string command, string arguments)
        {
            Process p = Process.Start(command, arguments);

            //Process p = new Process();
            //p.StartInfo.UseShellExecute = false;
            //p.StartInfo.FileName = command;
            //p.StartInfo.Arguments = arguments;
            //p.StartInfo.CreateNoWindow = true;
            //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            
            //p.Start();
            //p.WaitForExit();
            Thread.Sleep(1000);

            return 0;// p.ExitCode;
        }

        public static string FilenameFromTitle(string name)
        {
            //// first trim the raw string
            //string safe = name.Trim();

            //// replace spaces with hyphens
            //safe = safe.Replace(" ", "-").ToLower();

            //// replace any 'double spaces' with singles
            //if (safe.IndexOf("--") > -1)
            //    while (safe.IndexOf("--") > -1)
            //        safe = safe.Replace("--", "-");

            //// trim out illegal characters
            //safe = Regex.Replace(safe, "[^a-z0-9\\-]", "");

            //// trim the length
            //if (safe.Length > 50)
            //    safe = safe.Substring(0, 49);

            //// clean the beginning and end of the filename
            //char[] replace = { '-', '.' };
            //safe = safe.TrimStart(replace);
            //safe = safe.TrimEnd(replace);

            //return safe;

		    // Remove year and postfix
		    string ret = Regex.Replace(name, "(19\\d{2}|2\\d{3}|1080|720|DVDrip|HDRIP|xvid|R5|BRRIP|BluRay|x264|BDRIP|HDTV|DSR).*$", string.Empty, RegexOptions.IgnoreCase);

		    // Remove download type [RS.com], etc.
		    ret = Regex.Replace(ret, "\\[[^\\]]*\\]", string.Empty);

		    // Remove panctuation and redundant spaces
		    const string panctuationRegex = "[ \\t\\.\\-_():;{}~\\*/|><&]+";
		    ret = Regex.Replace(ret, panctuationRegex, " ");

		    // Trim the string
		    ret = ret.Trim();

		    // If we got nothing, try again, just remove panctuation (shouldn't happen, but can...)
		    if (ret.Length == 0)
		    {
			    // Remove panctuation and redundant spaces
			    ret = Regex.Replace(name, panctuationRegex, " ");

			    // Trim the string
			    ret = ret.Trim();
		    }

		    // Capitalize (Title case)
		    TextInfo UsaTextInfo = new CultureInfo("en-US", false).TextInfo;
		    ret = UsaTextInfo.ToTitleCase(ret);

		    // return manipulated string
		    return ret;
        }

        public static string CleanName(string name)
        {
            int pos = name.IndexOf(".heb", StringComparison.InvariantCultureIgnoreCase);
            if (pos >= 0)
                name = name.Substring(0, pos);

            return name.Replace("-", " ").Replace(".", " ").Replace("[", " ").Replace("]", " ").Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace("  ", " ");
        }

    }
}
